package com.fwixvye.service.impl;

import com.fwixvye.pojo.Test;
import com.fwixvye.mapper.TestMapper;
import com.fwixvye.service.ITestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;

@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {

    @Autowired
    TestMapper testMapper;

    class Request {
        Integer code;
        CompletableFuture<Test> future;
    }

    BlockingQueue<Request> blockingQueue = new LinkedBlockingQueue<>();

    @PostConstruct
    public void init() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            int size = blockingQueue.size();
            if (size == 0) {
                return;
            }
            ArrayList<Request> requests = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Request request = blockingQueue.poll();
                requests.add(request);
            }
            System.out.println("这批同时处理了" + requests.size() + "个请求");
            ArrayList<Integer> codes = new ArrayList<>();
            for (Request request : requests) {
                codes.add(request.code);
            }
            List<Test> tests = testMapper.selectBatchIds(codes);
            HashMap<Integer, Test> hashMap = new HashMap<>();
            for (Test test : tests) {
                Integer id = test.getId();
                hashMap.put(id, test);
            }
            for (Request request : requests) {
                Test test = hashMap.get(request.code);
                request.future.complete(test);
            }
        }, 0, 10, TimeUnit.MILLISECONDS);
    }


    @Override
    public Test testRole(Integer code) throws ExecutionException, InterruptedException {
        Request request = new Request();
        request.code = code;
        CompletableFuture<Test> future = new CompletableFuture<>();
        request.future = future;

        blockingQueue.add(request);
//        System.out.println(future.get());
        return future.get();


    }


//    class Request {
//        Integer code;
//        CompletableFuture<Test> future;
//    }
//
//    BlockingQueue<Request> blockingQueue = new LinkedBlockingQueue<>();//队列用来存请求
//
//    @PostConstruct
//    public void init() {
//        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);//创建定时任务线程池(计划执行器服务)
//        scheduledExecutorService.scheduleAtFixedRate(() -> {//按 固定 比率 安排
//
//            int size = blockingQueue.size();
//            if (size == 0) {
//                return;
//            }
//
////            System.out.println(size);
//
//            ArrayList<Request> requests = new ArrayList<>();
//            for (int i = 0; i < size; i++) { // 拿到 blockingQueue 里面的 Request
//                Request request = blockingQueue.poll();
//                requests.add(request);
//            }
//
//            System.out.println("批量处理了：" + size + "条数据");
//
//            ArrayList<Integer> codes = new ArrayList<>();
//            for (Request request : requests) { // 拿到 request 里面的 code
//                codes.add(request.code);
//            }
//
//            List<Test> tests = testMapper.selectBatchIds(codes);
//
//            //请求分发
//            HashMap<Integer, Test> hashMap = new HashMap<>();
//            for (Test test : tests) {
//                Integer id = test.getId();
//                hashMap.put(id, test);
//            }
//
//            //返回请求
//            for (Request request : requests) {
//                Test test = hashMap.get(request.code);
//                request.future.complete(test);
//            }
//
//
//        }, 0, 10, TimeUnit.MILLISECONDS);
//    }
//
//
//    public void testRole(Integer code) throws ExecutionException, InterruptedException {
//
//        Request request = new Request();
//        request.code = code;
//        CompletableFuture<Test> future = new CompletableFuture<>();
//        request.future = future;
//        blockingQueue.add(request);
////        System.out.println(future.get());
//    }


}
